package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateGroup {
	
	private static Logger LOG = Logger.getLogger(DateGroup.class);
	private static String DATE_FORMAT_2 = "MM-dd-yy";
	private static String DATE_FORMAT_1 = "yyyy-MM-dd";
	private static String DATE_FORMAT_3 = "yy-MM-dd";
	
	public static String getNow() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
	
	/**
	 * to standard the form of the date, change 2012-1-1 to 2012-01-01
	 * @param date
	 * @return
	 */
	public static java.sql.Date formateDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_1);
		java.sql.Date resultDate = null;
		try {
			Date d = sdf.parse(date);
			resultDate = java.sql.Date.valueOf(sdf.format(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resultDate;
	}
	
	public static String formateDateStr(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_1);
		StringBuilder resultDate = new StringBuilder();
		try {
			Date d = sdf.parse(date);
			resultDate.append(sdf.format(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resultDate.toString();
	}

	/**
	 * @author suwy
	 * @date 2013-7-4
	 * @todo 日期转换 - 月日年 to 年月日
	 */
	public static String MMddYYtoStanderd(String date) {
		SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT_2);
		SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT_1);
		java.util.Date date1 = null;
		try {
			date1 = sdf1.parse(date);
		} catch (ParseException e) {
			LOG.info("日期转换 - 月日年 to 年月日：出错 ！" + e);
		}
		
		return sdf2.format(date1).toString();
	}
	
	public static String ToDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT_1);
		SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT_3);
		java.util.Date date1 = null;
		try {
			date1 = sdf1.parse(new String(c));
		} catch (ParseException e) {
			LOG.info("全角转半角：出错 ！" + e);
		}
		
		return sdf2.format(date1).toString();
	}
	
//	public static void main(String[] args) {
//		LOG.info(ToDBC("０５-３-１５"));
//	}
}
